﻿#include "ItemRentingDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../Settings.h"
#include "../../MirSounds/SoundManager.h"
#include "../../../Shared/Enums.h"
#include "../../MirNetwork/Network.h"
#include "../../../Shared/ClientPackets.h"
#include "../../MirControls/MirInputBox.h"
#include "../GameScene.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirSounds;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{

UserItem *ItemRentingDialog::RentalItem;

    ItemRentingDialog::ItemRentingDialog() : _lockButton(new MirButton();
        _lockButton->setIndex(250);
        _lockButton->setHoverIndex(251);
        Point tempVar(18, 76);
        _lockButton->setLocation(&tempVar);
        Size tempVar2(28, 25);
        _lockButton->setSize(&tempVar2);
        _lockButton->setLibrary(Libraries::Prguse);
        _lockButton->setParent(this);
        _lockButton->setPressedIndex(252);
        _lockButton->setSound(SoundList::ButtonA)), _confirmButton(new MirButton();
        _confirmButton->setIndex(10);
        _confirmButton->setHoverIndex(11);
        Point tempVar3(130, 76);
        _confirmButton->setLocation(&tempVar3);
        Size tempVar4(58, 28);
        _confirmButton->setSize(&tempVar4);
        _confirmButton->setLibrary(Libraries::Prguse3);
        _confirmButton->setParent(this);
        _confirmButton->setPressedIndex(12);
        _confirmButton->setSound(SoundList::ButtonA);
        _confirmButton->setEnabled(false))
        {
        setIndex(238);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        Size tempVar5(204, 109);
        setSize(&tempVar5);
        Point tempVar6(Settings::ScreenWidth - getSize()->Width - getSize()->Width / 2, getSize()->Height * 2 + getSize()->Height / 2 + 15);
        setLocation(&tempVar6);
        setSort(true);

        // Confirm Button

        _confirmButton->Click += [&] (o, e)
        {
            C::ConfirmItemRental tempVar7();
            Network::Enqueue(&tempVar7);
        };

        // Close Button

        auto closeButton = new MirButton();
        closeButton->setHoverIndex(361);
        closeButton->setIndex(360);
        Point tempVar8(180, 3);
        closeButton->setLocation(&tempVar8);
        closeButton->setLibrary(Libraries::Prguse2);
        closeButton->setParent(this);
        closeButton->setPressedIndex(362);
        closeButton->setSound(SoundList::ButtonA);
        closeButton->Click += [&] (sender, args)
        {
            CancelItemRental();
        };

        // Lock Button

        _lockButton->Click += [&] (o, e)
        {
            if (RentalPeriod < 1 || RentalPeriod > 30)
            {
                return;
            }
    
            C::ItemRentalLockItem tempVar9();
            Network::Enqueue(&tempVar9);
        };

        // Set Rental Period Button

        _setRentalPeriodButton = new MirButton();
        _setRentalPeriodButton->setIndex(7);
        _setRentalPeriodButton->setHoverIndex(8);
        Point tempVar10(46, 76);
        _setRentalPeriodButton->setLocation(&tempVar10);
        Size tempVar11(84, 28);
        _setRentalPeriodButton->setSize(&tempVar11);
        _setRentalPeriodButton->setLibrary(Libraries::Prguse3);
        _setRentalPeriodButton->setParent(this);
        _setRentalPeriodButton->setPressedIndex(9);
        _setRentalPeriodButton->setSound(SoundList::ButtonA);
        _setRentalPeriodButton->Click += [&] (o, e)
        {
            InputRentalPeroid();
        };

        // Name Label

        _nameLabel = new MirLabel();
        _nameLabel->setParent(this);
        Point tempVar12(30, 8);
        _nameLabel->setLocation(&tempVar12);
        Size tempVar13(150, 14);
        _nameLabel->setSize(&tempVar13);
        _nameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        _nameLabel->setNotControl(true);

        // Rental Period Label

        _rentalPeriodLabel = new MirLabel();
        _rentalPeriodLabel->setParent(this);
        Point tempVar14(60, 42);
        _rentalPeriodLabel->setLocation(&tempVar14);
        Size tempVar15(150, 14);
        _rentalPeriodLabel->setSize(&tempVar15);
        _rentalPeriodLabel->setDrawFormat(TextFormatFlags->Left | TextFormatFlags::VerticalCenter);
        _rentalPeriodLabel->setNotControl(true);

        // Item Cell

        ItemCell = new MirItemCell();
        ItemCell->setBorderColour(Color::Lime);
        ItemCell->setGridType(MirGridType::Renting);
        ItemCell->setLibrary(Libraries::Items);
        ItemCell->setParent(this);
        Point tempVar16(16, 35);
        ItemCell->setLocation(&tempVar16);
        ItemCell->setItemSlot(0);

        delete closeButton;
        }

    void ItemRentingDialog::CancelItemRental()
    {
        C::CancelItemRental tempVar();
        Network::Enqueue(&tempVar);
    }

    void ItemRentingDialog::EnableConfirmButton()
    {
        _confirmButton->setEnabled(true);
    }

    void ItemRentingDialog::InputRentalPeroid()
    {
        auto inputBox = new MirInputBox(StringHelper::formatSimple("How long would you like to rent {0} to {1} for? (1 to 30 days).", RentalItem->getFriendlyName(), GameScene::Scene->GuestItemRentDialog->getGuestName()));

        inputBox->OKButton->Click += [&] (o1, e1)
        {
            if (!unsigned int::TryParse(inputBox->InputTextBox->getText(), RentalPeriod))
            {
                return;
            }
    
            if (RentalPeriod < 1 || RentalPeriod > 30)
            {
                return;
            }
    
            RefreshInterface();
            inputBox->Dispose();
    
            C::ItemRentalPeriod *tempVar = new C::ItemRentalPeriod();
            tempVar->Days = RentalPeriod;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };

        inputBox->Show();

        delete inputBox;
    }

    void ItemRentingDialog::RefreshInterface()
    {
        _nameLabel->setText(GameScene::getUser()->Name);
        _rentalPeriodLabel->setText(StringHelper::formatSimple("Rental Period: {0} Days", RentalPeriod));

        GameScene::Scene->GuestItemRentDialog->RefreshInterface();
        GameScene::Scene->GuestItemRentingDialog->RefreshInterface();

        Redraw();
    }

    void ItemRentingDialog::OpenItemRentalDialog()
    {
        GameScene::Scene->InventoryDialog->Show();

        Show();
        RefreshInterface();
        GameScene::Scene->GuestItemRentDialog->Show();
    }

    void ItemRentingDialog::Reset()
    {
        RentalItem = nullptr;
        RentalPeriod = 0;
        _confirmButton->setEnabled(false);
        GameScene::getUser()->RentalGoldAmount = 0;

        GameScene::Scene->GuestItemRentingDialog->Reset();

        Unlock();
        RefreshInterface();
        Hide();
    }

    void ItemRentingDialog::Lock()
    {
        _lockButton->setIndex(253);
        _lockButton->setEnabled(false);
        _setRentalPeriodButton->setEnabled(false);

        RefreshInterface();
    }

    void ItemRentingDialog::Unlock()
    {
        _lockButton->setIndex(250);
        _lockButton->setEnabled(true);
        _setRentalPeriodButton->setEnabled(true);
    }

UserItem *GuestItemRentingDialog::GuestLoanItem;

    GuestItemRentingDialog::GuestItemRentingDialog() : _nameLabel(new MirLabel();
        _nameLabel->setParent(this);
        Point tempVar(30, 8);
        _nameLabel->setLocation(&tempVar);
        Size tempVar2(150, 14);
        _nameLabel->setSize(&tempVar2);
        _nameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        _nameLabel->setNotControl(true)), _rentalPeriodLabel(new MirLabel();
        _rentalPeriodLabel->setParent(this);
        Point tempVar3(60, 42);
        _rentalPeriodLabel->setLocation(&tempVar3);
        Size tempVar4(150, 14);
        _rentalPeriodLabel->setSize(&tempVar4);
        _rentalPeriodLabel->setDrawFormat(TextFormatFlags->Left | TextFormatFlags::VerticalCenter);
        _rentalPeriodLabel->setNotControl(true);
        _rentalPeriodLabel->setText("Rental Period: 0 Days")), _lockButton(new MirButton();
        _lockButton->setIndex(250);
        Point tempVar5(18, 76);
        _lockButton->setLocation(&tempVar5);
        Size tempVar6(28, 25);
        _lockButton->setSize(&tempVar6);
        _lockButton->setLibrary(Libraries::Prguse);
        _lockButton->setParent(this);
        _lockButton->setEnabled(false)), _setRentalPeriodButton(new MirButton();
        _setRentalPeriodButton->setIndex(7);
        Point tempVar7(46, 76);
        _setRentalPeriodButton->setLocation(&tempVar7);
        Size tempVar8(84, 28);
        _setRentalPeriodButton->setSize(&tempVar8);
        _setRentalPeriodButton->setLibrary(Libraries::Prguse3);
        _setRentalPeriodButton->setParent(this);
        _setRentalPeriodButton->setEnabled(false)), _confirmButton(new MirButton();
        _confirmButton->setIndex(10);
        Point tempVar9(130, 76);
        _confirmButton->setLocation(&tempVar9);
        Size tempVar10(58, 28);
        _confirmButton->setSize(&tempVar10);
        _confirmButton->setLibrary(Libraries::Prguse3);
        _confirmButton->setParent(this);
        _confirmButton->setEnabled(false))
        {
        setIndex(238);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        Size tempVar11(204, 109);
        setSize(&tempVar11);
        Point tempVar12(Settings::ScreenWidth - getSize()->Width - getSize()->Width / 2, getSize()->Height * 2 + getSize()->Height / 2 + 15);
        setLocation(&tempVar12);
        setSort(true);






        _guestItemCell = new MirItemCell();
        _guestItemCell->setBorderColour(Color::Lime);
        _guestItemCell->setGridType(MirGridType::GuestRenting);
        _guestItemCell->setLibrary(Libraries::Items);
        _guestItemCell->setParent(this);
        Point tempVar13(16, 35);
        _guestItemCell->setLocation(&tempVar13);
        _guestItemCell->setItemSlot(0);
        }

    void GuestItemRentingDialog::RefreshInterface()
    {
        _nameLabel->setText(_guestName);
        _rentalPeriodLabel->setText(StringHelper::formatSimple("Rental Period: {0} Days", GuestRentalPeriod));

        if (GuestLoanItem != nullptr)
        {
            GameScene::Bind(GuestLoanItem);
        }

        Redraw();
    }

    void GuestItemRentingDialog::Reset()
    {
        Unlock();
        GuestLoanItem = nullptr;
        _guestName = "";

        Hide();
    }

    void GuestItemRentingDialog::SetGuestName(const std::string &name)
    {
        _guestName = name;
    }

    void GuestItemRentingDialog::Lock()
    {
        _lockButton->setIndex(253);

        RefreshInterface();
    }

    void GuestItemRentingDialog::Unlock()
    {
        _lockButton->setIndex(250);
    }
}
